package com.controller;

import com.pojo.*;
import com.service.CommunityService;
import com.service.ServiceListService;
import com.service.ServiceOrderService;
import com.service.ServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/service")
@CrossOrigin
public class ServiceController {
    @Autowired
    private CommunityService communityService;
    @Autowired
    private ServiceTypeService serviceTypeService;
    @Autowired
    private ServiceListService serviceListService;
    @Autowired
    private ServiceOrderService serviceOrderService;

    private Result result=new Result();


    @RequestMapping("community/findAll")
    public Result findaAll(){
        List<Community> list = communityService.findAll();
        if(list!=null){
            result.setCode(200);
            result.setMsg("社区查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("社区查询失败");
        }
        return result;
    }
    @RequestMapping("/community/findById")
    public Result findById(Integer id){
        Community community=communityService.findById(id);
        System.out.println(id);
        if(community!=null){
            result.setCode(200);
            result.setMsg("社区id查询成功");
            result.setData(community);
        }else {
            result.setCode(500);
            result.setMsg("社区id查询失败");
        }
        return result;
    }
    @RequestMapping("/type/findall")
    public Result findall(){
        List<ServiceType> list = serviceTypeService.typeFindAll();
        if(list!=null){
            result.setCode(200);
            result.setMsg("服务分类查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("服务分类查询失败");
        }
        return result;
    }
    @RequestMapping("/findByTypeandCid/{id}/{cid}")
    public Result findByTypeandCid(@PathVariable Integer id, @PathVariable Integer cid){
        ServiceType serviceType=serviceTypeService.findByTypeId(id);
        List<ServiceItem> list=serviceListService.findByTypeandCid(serviceType.getType(),cid);
        if(list!=null){
            result.setCode(200);
            result.setMsg("分类服务显示查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("分类服务显示查询失败");
        }
        return result;
    }
    @RequestMapping("/order/add")
    public Result add(@RequestBody ServiceOrder serviceOrder, HttpSession session){
        System.out.println(serviceOrder.getAppointmentTime());
        boolean flag= serviceOrderService.add(serviceOrder);
        if(flag){
            result.setCode(200);
            result.setMsg("添加数据成功");

        }else{
            result.setCode(500);
            result.setMsg("添加数据失败");
        }
        return result;

    }
    @RequestMapping("/order/findAll/{username}")
    public Result findAll(@PathVariable String username){
        List<ServiceOrder> list=serviceOrderService.findAll(username);
        if(list!=null){
            result.setCode(200);
            result.setMsg("服务订单成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("服务订单失败");
        }
        return result;
    }
    @RequestMapping("/goods/findAll/{cid}")
    public Result goodsFindAll(@PathVariable Integer cid){
        List<ServiceItem> list=serviceListService.goodsFindAll(cid);
        if(list!=null){
            result.setCode(200);
            result.setMsg("全部服务查询成功");
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("全部服务查询失败");
        }
        return result;
    }
    @RequestMapping("/order/cancel/{id}")
    public Result orderCancel(@PathVariable Integer id){
        boolean flag=serviceOrderService.updateById(id);
        if(flag){
            result.setCode(200);
            result.setMsg("修改数据成功");

        }else{
            result.setCode(500);
            result.setMsg("修改数据失败");
        }
        return result;
    }
    @RequestMapping("/order/del/{id}")
    public Result orderDel(@PathVariable Integer id){
        boolean flag=serviceOrderService.delById(id);
        if(flag){
            result.setCode(200);
            result.setMsg("删除数据成功");

        }else{
            result.setCode(500);
            result.setMsg("删除数据失败");
        }
        return result;
    }
}
